package com.lagou.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class AbsoluteResourceUtil {

    //返回静态资源绝对路径
    public static String getAbsoluteUrl(String url) {

        String rootPath = AbsoluteResourceUtil.class.getResource("/").getPath();
        String absolutePath = rootPath.replaceAll("\\\\", "/") + url;
        return absolutePath;

    }

    public static void getResource(InputStream inputStream, OutputStream outputStream) throws IOException {
        int writen = 0;//已读的字节长度
        int byteSize = 1024;//每次读取的字节长度
        int count = 0;
        while(count == 0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[byteSize];
        //先输出请求头
        outputStream.write(HttpProtocolUtil.httpResponseHeader200(count).getBytes());

        while(writen < count){
            if(writen + byteSize > count){
                byteSize = count - writen;
                bytes = new byte[byteSize];
            }

            inputStream.read(bytes);
            outputStream.write(bytes);

            outputStream.flush();
            writen += byteSize;
        }
    }
}
